#include "StoryDuplicate.h"
#include "Duplicate/DuplicateMgr.h"
#include "Character/HuoYueDuPart.h"

StoryDuplicate::StoryDuplicate()
{
	m_mapPlayerPick.clear();
	m_mapDupCharInfo.clear();
	m_byType = (int8_t)EDuplicateType::EDuplicateType_Story;
}

StoryDuplicate::~StoryDuplicate()
{

}


//初始化
bool StoryDuplicate::Init(uint32_t nDuplicateID, uint32_t nSceneID, IBrushMonster *pBrush)
{
	return Duplicate::Init(nDuplicateID, nSceneID, pBrush);
}
//
void StoryDuplicate::UnInit()
{
	Duplicate::UnInit();
	m_mapPlayerPick.clear();
	m_mapDupCharInfo.clear();
}


//生物进入场景
void StoryDuplicate::OnCreatureEnter(Creature *pCreature)
{
}

//生物离开副本
void StoryDuplicate::OnPlayerLeave(Player *pPlayer)
{
}

//是否能进入
bool StoryDuplicate::OnCanEnter(Creature *pCreature)
{
	pCreature = pCreature;
	return true;
}

//进入区域
bool StoryDuplicate::OnEnterArea(uint32_t nAreaID)
{
	nAreaID = nAreaID;
	return true;
}
//复活
void StoryDuplicate::OnRelive(Player *pPlayer, int8_t byType)
{
	_UNUSE(pPlayer);
	_UNUSE(byType);
}

//拾取掉落
void StoryDuplicate::PickDrop(CharIDType cid, string &strName, int32_t prof, int32_t sex, VEC_ITEM_PROTO_EX &vecPickDrop)
{
	CompareDupFunc *pDupFunc = g_GetItemMgr()->GetDupFunc();
	if (nullptr == pDupFunc)
	{
		return;
	}

	VEC_ITEM_PROTO_EX *pVecCharDrop = nullptr;
	PickUpDropMap::iterator iter = m_mapPlayerPick.find(cid);
	if (iter != m_mapPlayerPick.end())
	{
		pVecCharDrop = &iter->second;
	}
	else
	{
		VEC_ITEM_PROTO_EX vecPickItem;
		vecPickItem.clear();
		m_mapPlayerPick[cid] = vecPickItem;
		pVecCharDrop = &m_mapPlayerPick[cid];
	}
	if (nullptr == pVecCharDrop)
	{
		return;
	}
	MapDupCharInfo::iterator iterInfo = m_mapDupCharInfo.find(cid);
	if (iterInfo == m_mapDupCharInfo.end())
	{
		SDupCharInfo info;
		info.strName = strName;
		info.prof = prof;
		info.sex = sex;
		m_mapDupCharInfo[cid] = info;
	}
	//
	VEC_ITEM_PROTO_EX::iterator iterPick = vecPickDrop.begin();
	for (; iterPick != vecPickDrop.end(); ++iterPick)
	{
		pVecCharDrop->push_back((*iterPick));
	}
	/*//排序
	std::sort(std::begin(*pVecCharDrop), std::end(*pVecCharDrop), *pDupFunc);
	//移除多余的
	int32_t isize = pVecCharDrop->size();
	while (isize > DUP_FINISH_DROP_NUM)
	{
		int32_t idx = isize - 1;
		pVecCharDrop->erase(pVecCharDrop->begin() + idx);
		--isize;
	}*/
}

//设置副本掉落数据
void StoryDuplicate::SetDropData(DupAllDropProto &protoData)
{
	PickUpDropMap::iterator iter = m_mapPlayerPick.begin();
	for (; iter != m_mapPlayerPick.end(); ++iter)
	{
		DupSingleDropProto *proto = protoData.add_drop_info();
		if (nullptr == proto)
		{
			continue;
		}
		proto->set_charid(iter->first);
		MapDupCharInfo::iterator iterInfo = m_mapDupCharInfo.find(iter->first);
		if (iterInfo != m_mapDupCharInfo.end())
		{
			SDupCharInfo &info = iterInfo->second;
			proto->set_name(info.strName);
			proto->set_prof(info.prof);
			proto->set_sex(info.sex);
		}
		//
		VEC_ITEM_PROTO_EX &vecPick = iter->second;
		VEC_ITEM_PROTO_EX::iterator iterVec = vecPick.begin();
		for (; iterVec != vecPick.end(); ++iterVec)
		{
			ItemProtoInfo *protoItem = proto->add_pick();
			if (nullptr != protoItem)
			{
				protoItem->CopyFrom((*iterVec));
			}
		}
	}
}


//定时器更新
void StoryDuplicate::OnTimerUpdate(uint32_t nTimerID)
{
	switch (nTimerID)
	{
	case ETimer_Flop:
		{
			//翻牌结束
			OnCommon_FlopStateEnd();
		}
		break;
	case ETimer_Clean:
		{
			OnCommon_CleanStateEnd();
		}
		break;
	case ETimer_Destory:
		{
			LogDebugFmtPrint("[logic] StoryDuplicate destory .... duplicateid:%u , sceneid:%u, mapid:%u ", m_nDuplicateID, m_nSceneID, m_nMapID);
			//回收
			OnCommon_DestoryStateEnd();
		}
		break;
	default:
		break;
	}
	
}
//执行事件
void StoryDuplicate::OnExecuteEvent(uint16_t nEventID, uint32_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_CREATURE_DEAD:
	{
	}
	break;
	case EVENT_ENTER_SCENE:
	{
	}
	break;
	case EVENT_DISCONNECT:
	{
	}
	break;
	}
}

void StoryDuplicate::OnReady_State()
{

}

void StoryDuplicate::OnBegin_State()
{
	OnCommon_DupBeginState();
}

void StoryDuplicate::OnFinish_State()
{
	OnCommon_DupFinishState();
}

void StoryDuplicate::OnFlop_State()
{
	//设置抽奖时间 15s
	LogDebugFmtPrint("[logic] StoryDuplicate::OnDuplicateState_Flop....duplicateid:%u , sceneid:%u ", m_nDuplicateID, m_nSceneID);
	OnCommon_DupFlopState();
}

void StoryDuplicate::OnClean_State()
{
	OnCommon_DupCleanState();
}

void StoryDuplicate::OnDestory_State()
{
	OnCommon_DupDestoryState();
}